///////////////////////////
//
// File: bcf_menu_noise.cpp
//
// Author: Brian Fehrman
//
// Purpose: Contains the class implementation for the Noise Menu
//
///////////////////////////

#include "bcf_img_proc.h"

bool BCF_Img_Proc::Menu_Noise_Add_Gaussian_Noise(Image &image)
{
    if ( image.IsNull() ) return false;

    double sigma = 1.0;

    //Create gamma dialog box and get value
    Dialog dlg("Specifiy Sigma");
    dlg.Add(sigma, "Sigma");

    if( dlg.Show() )
    {

        gaussianNoise( image, sigma );
    }

    return true;
}


bool BCF_Img_Proc::Menu_Noise_Add_Impulse_Noise(Image &image)
{
    if ( image.IsNull() ) return false;

    int probability = 1;

    //Create gamma dialog box and get value
    Dialog dlg( "Specifiy Probability" );
    dlg.Add( probability, "Probability" );

    if( dlg.Show() )
    {

        impulseNoise( image, probability );
    }

    return true;
}

///////////////////////////
// Function: Noise_Noise_Clean
//
// Author: Brian Fehrman
//
// Purpose: Cleans noise in image using the out of range algorithm
///////////////////////////
bool BCF_Img_Proc::Menu_Noise_Noise_Clean( Image &image )
{
    if ( image.IsNull() ) return false;

    int nrows = image.Height();
    int ncols = image.Width();
    int NSIZE = 3;
    float **mean_filter;
    int threshold = 35;

    Dialog dlg( "Choose Threshold" );
    dlg.Add( threshold, "Threshold" );

    if( dlg.Show() )
    {
        Image mean_image = image;

        //Create the mean filter, which is passed to the Apply_Correlation_Filter function
        mean_filter = new float*[ NSIZE ];

        for( int row = 0; row < NSIZE; row++ )
        {
            mean_filter[ row ] = new float[ NSIZE ];
        }

        for( int row = 0; row < NSIZE; row++ )
        {
            for( int col = 0; col < NSIZE; col++ )
            {
                mean_filter[ row ][ col ] = 1.0 / ( NSIZE * NSIZE );
            }
        }

        Apply_Correlation_Filter( mean_image, mean_filter, NSIZE );

        for( int row = 0; row < NSIZE; row++ )
        {
           delete mean_filter[ row ];
        }

        delete [] mean_filter;

        nsize_div_2 = NSIZE / 2;

        //Traverse each pixel in matrix, being careful not to overstep bounds
        for( int img_row= 0; img_row < ( nrows ); img_row++ )
        {
            for( int img_col = 0; img_col < ( ncols); img_col++ )
            {
                int diff = abs( image[ img_row ][img_col ] - mean_image[ img_row ][img_col ] );

                if( diff > threshold )
                {
                    image[ img_row ][img_col ] = mean_image[ img_row ][img_col ];
                }
            }//end img_col loop

        }//end img_row loop
    }

    return true;
}
